//
//  HNPaySuccessController.m
//  baidaifu
//
//  Created by 朱传正 on 2016/12/24.
//  Copyright © 2016年 好牛. All rights reserved.
//

#import "HNPaySuccessController.h"
#import "NSString+Size.h"
#import "ChatViewController.h"

@interface HNPaySuccessController ()

@property (weak, nonatomic) IBOutlet UILabel *orderSn;

@property (weak, nonatomic) IBOutlet UILabel *orderName;

@property (weak, nonatomic) IBOutlet UILabel *orderPresonName;

@property (weak, nonatomic) IBOutlet UILabel *orderTime;

@property (weak, nonatomic) IBOutlet UILabel *orderPrice;


@end

@implementation HNPaySuccessController

- (IBAction)flashQuickAction:(UIButton *)sender {
    
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",_orderSn.text,@"orderSn", nil];
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_consultService_start parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSArray *urerList = [data objectForKey:@"obj"];
            NSLog(@"------obj = %@",urerList);
            
            //开始聊天
            NSString *sellerUsername = [NSString stringWithFormat:@"%@",[_orderDic objectForKey:@"sellerUsername"]];
            
            if(sellerUsername.length <= 1){
                [self showHint:@"无此用户"];
            }
            NSString *loginName = [[EMClient sharedClient] currentUsername];
            if ([sellerUsername isEqualToString:loginName]) {
                [self showHint:@"不能和自己聊天"];
                return;
            }else{
                
                [self setsellerUsername:sellerUsername];
                
            }
            
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            
            [self showHint:info];
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取用户信息失败"];
    }];
    
}

-(void)setsellerUsername:(NSString *)mobile {
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:mobile,@"mobile", nil];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_userInfoList_tel parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSArray *urerList = [data objectForKey:@"obj"];
            NSLog(@"------obj = %@",urerList);
            
            NSDictionary *userInfo = urerList[0];
            
            
            NSString *nickname = nil;
            NSString *photo = nil;
            NSString *hnLoginName = nil;
            
            nickname = [NSString stringWithFormat:@"%@",[userInfo objectForKey:@"nickname"]];
            
            photo = [NSString stringWithFormat:@"%@%@",url_Base,[userInfo objectForKey:@"photo"]];
            
            hnLoginName = mobile;
            NSMutableDictionary *peopleUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:hnLoginName,@"hnLoginName" ,nil];
            [peopleUserInfo setObject:nickname forKey:@"nickname"];
            [peopleUserInfo setObject:photo forKey:@"photo"];
            NSLog(@"%@",peopleUserInfo);
            
            [AppConfig savePeopleInfo:peopleUserInfo];
            
            ChatViewController *chatController = [[ChatViewController alloc] initWithConversationChatter:mobile conversationType:EMConversationTypeChat];
            chatController.title = nickname;
            
            [self.navigationController pushViewController:chatController animated:YES];
            
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            NSLog(@"msg = %@",info);
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取用户信息失败"];
    }];
    
}

- (IBAction)enterOrderDetailAction:(UIButton *)sender {
    
    int index=(int)[[self.navigationController viewControllers]indexOfObject:self];
    
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index-2]animated:YES];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"支付成功";
    NSDictionary *goodsDic = [_orderDic objectForKey:@"goods"][0];
    NSLog(@"------goods = %@",goodsDic);
    _orderSn.text = [NSString stringWithFormat:@"%@",[_orderDic objectForKey:@"orderSn"]];
    _orderName.text = [NSString stringWithFormat:@"%@",[goodsDic objectForKey:@"goodsName"]];
    _orderPresonName.text = [NSString stringWithFormat:@"%@",[goodsDic objectForKey:@"goodsName"]];
    _orderTime.text = [NSString stringWithFormat:@"%@",[_orderDic objectForKey:@"createTime"]];
    _orderPrice.text = [NSString stringWithFormat:@"¥%@",[_orderDic objectForKey:@"money"]];
    
    

    
}


-(void)drawView{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"xiangyou.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
    
    
    //    self.lineLabel.backgroundColor = [UIColor redColor];
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGColorRef borderColor = CGColorCreate(colorSpaceRef, (CGFloat[]){193,193,193,1});
    self.lineLabel.layer.borderWidth = 0.5;
    self.lineLabel.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor grayColor]);
    
    [self.lineLabel.layer setBorderColor:borderColor];
    
    
    
    CGColorRef bColor = CGColorCreate(colorSpaceRef, (CGFloat[]){226.0/255.0,226.0/255.0,226.0/255.0,1});
    _orderDetailBtn.layer.masksToBounds = YES;
    _orderDetailBtn.layer.borderColor = bColor;
    _orderDetailBtn.layer.borderWidth = 1;
    
    //    _detailLabel.text = @"1.图文咨询是按次收费的，七天内有效，所以请小伙伴们把握好时间\n2.咨询时间到了可以继续购买此服务，继续和医生交流";
    
    _detailLabel.backgroundColor = [UIColor whiteColor];
    
    CGFloat height = [_detailLabel.text heightWithFont:[UIFont systemFontOfSize:13.0f] constrainedToWidth:((_detailLabel.frame.size.width))];
    CGRect frame = _detailLabel.frame;
    frame.size.height = height;
    _detailLabel.frame = frame;
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, 0.5)];
    lineLabel.backgroundColor = [UIColor grayColor];
    [self.view addSubview:lineLabel];
    
    
}

-(void)backAction{
    int index=(int)[[self.navigationController viewControllers]indexOfObject:self];
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index-2]animated:YES];
}


@end
